using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Unity.Per.Common.UIFrameWork
{
    public abstract class BaseWindowSlot :BaseModuleMono{

        public RectTransform BindWindowRegion;

        [SerializeField]
        private List<BindWindow> m_BindWindowList;
        public List<BindWindow> BindWindowList {
            get {
                if ( m_BindWindowList == null ) {
                    m_BindWindowList = new List<BindWindow>();
                }
                return m_BindWindowList;
            }
        }

        BindWindow m_currentBindWindow;
        public BindWindow CurrentBindWindow {
            get {
                return m_currentBindWindow;
            }
        }

        private BaseEntery m_entry;
        public BaseEntery entry {
            get {
                if ( m_entry == null ) {
                    UpdateEntry(GetComponentInParent<BaseEntery>());
                }
                return m_entry;
            }
            set {
                UpdateEntry(value);
            }
        }

        void UpdateEntry(BaseEntery entry) {
            if ( entry == m_entry )
                return;

            if ( m_entry ) {
                m_entry.RemoveModule(this);
                m_entry.RemoveBaseWindowSlot(this);
            }

            m_entry = entry;

            if ( m_entry ) {
                m_entry.AddModule(this);
                m_entry.AddBaseWindowSlot(this);
            }
        }

        protected override void OnModuleInit() {
            base.OnModuleInit();

            if ( BindWindowRegion ) {
                transform.SetParent(BindWindowRegion,false);
            }
        }

        protected override void OnModuleStart() {
            base.OnModuleStart();
            gameObject.SetActive(true);
        }

        protected override void OnModuleStop() {
            base.OnModuleStop();
            gameObject.SetActive(false);
        }


        public void AddBindWindow(BindWindow bindWindow) {
            if ( bindWindow == null )
                return;
            if ( BindWindowList.Contains(bindWindow) == false ) {
                BindWindowList.Add(bindWindow);
            }
        }

        public void RemoveBindWindow(BindWindow bindWindow) {
            if ( BindWindowList.Contains(bindWindow)) {
                BindWindowList.Remove(bindWindow);
            }
        }

        /// <summary>
        /// handle BindWindow
        /// </summary>
        /// <param name="bindWindow"></param>
        internal void Open(BindWindow bindWindow) {
            if ( bindWindow == null )
                return;

            if ( m_currentBindWindow ) {
                m_currentBindWindow.ModuleStop();
            }

            m_currentBindWindow = bindWindow;
            m_currentBindWindow.ModuleStart();

        }

        /// <summary>
        /// handle BindWindow
        /// </summary>
        internal void Close() {
            if ( m_currentBindWindow ) {
                m_currentBindWindow.ModuleStop();
            }
            m_currentBindWindow = null;
        }



    }
}
